﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;

namespace S7.Windows.Customers
{
    /// <summary>
    /// Interaction logic for EditAnAccount.xaml
    /// </summary>
    public partial class EditAnAccount : Window
    {
        public EditAnAccount()
        {
            InitializeComponent();
        }
		
		private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            Customer cust = new Customer();
            cust.CustomerID = (int)Customer.curCustomerID;
            cust.Password = tbPassword.Text;
            cust.UserName = tbUserName.Text;
            cust.FirstName = tbFirstName.Text;
            cust.LastName = tbLastName.Text;
            cust.Address = tbAddress.Text;
            cust.Mobile = tbMobile.Text;
            cust.Email = tbEmail.Text;
            Thread thr = new Thread(new ThreadStart(() =>
            {
                string error = cust.EditInfo();
                if (error.Equals(""))
                {
                    this.Dispatcher.Invoke(new ThreadStart(() =>
                    {
                        this.Close();
                    }), null);
                }
                else
                {
                    MessageBox.Show(error, "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                }
                }));
            thr.IsBackground = true;
            thr.Start();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Thread thr = new Thread(new ThreadStart(() =>
                {
                    var queryCust = from cust in Customer.lstCustomers
                                    where cust.CustomerID == Customer.curCustomerID
                                    select cust;
                    List<Customer> SelectedCust = queryCust.ToList();

                    this.Dispatcher.Invoke(new ThreadStart(() =>
                        {
                            tbUserName.Text = SelectedCust[0].UserName;
                            tbPassword.Text = SelectedCust[0].Password;
                            tbFirstName.Text = SelectedCust[0].FirstName;
                            tbLastName.Text = SelectedCust[0].LastName;
                            tbAddress.Text = SelectedCust[0].Address;
                            tbMobile.Text = SelectedCust[0].Mobile;
                            tbEmail.Text = SelectedCust[0].Email;
                            //cbLanguage
                        }), null);
                }));
            thr.IsBackground = true;
            try
            {
                thr.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
